<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\EventRecord;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Admin\Renderable\UserTable;
use App\Admin\Renderable\EventTable;
use App\Models\User;
use App\Models\Event;

class EventRecordController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $model = EventRecord::with(['eventer']);

        return Grid::make($model, function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');

            $grid->column('id')->sortable();
            $grid->column('user_id')->modal('用户详情', function () {
                return app('App\Admin\Controllers\UserController')->current($this->user_id);
            });
            $grid->column('event')->label();
            $grid->column('eventer.title', '事件标题');
            $grid->column('model_id');
            $grid->column('mt', '模型标题')->display(function ($column) {
                return $this->model_title;
            });
            $grid->column('created_at');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('id')->integer()->width(1);
                $filter->equal('model_id')->integer()->width(2);

                $filter->equal('user_id', '用户')->width(2)
                    ->selectTable(UserTable::make(['id' => ''])) // 设置渲染类实例，并传递自定义参数
                    ->title('用户列表')
                    ->dialogWidth('50%') // 弹窗宽度，默认 800px
                    ->model(User::class, 'id', 'name'); // 设置编辑数据显示

                $filter->equal('event', '事件')->width(2)
                    ->selectTable(EventTable::make(['label' => '']))
                    ->title('事件列表')
                    ->dialogWidth('50%')
                    ->model(Event::class, 'label', 'title');

                $filter->between('created_at')->datetime()->width(4);
            });

            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
            $titles = ['id' => 'ID', 'user_id' => '用户ID', 'event' => '事件', 'model_id' => '模型ID', 'created_at' => '创建时间'];

            $grid->export($titles)->csv();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new EventRecord(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('event');
            $show->field('model_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new EventRecord(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('event');
            $form->text('model_id');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
